﻿//Added : Kha

namespace VietThuongLMS.Utils.PdfReportGenerator
{
    using System.IO;


    using System.Diagnostics;
    using MigraDoc.DocumentObjectModel;
    using MigraDoc.Rendering;
    using MigraDoc.RtfRendering;
    using MigraDoc.DocumentObjectModel.Tables;
    using VietThuongLMS.ViewModels;

    using PdfSharp.Pdf;
    using System.Collections.Generic;
    using System;
    /// <summary>
    /// This class is responsible for rendering a html text string to a PDF document using the html renderer of iTextSharp.
    /// </summary>
    public class StandardPdfRenderer
    {
        private const int HorizontalMargin = 30;
        private const int VerticalMargin = 30;
        /// <summary>
        /// Defines the styles used in the document.
        /// </summary>
        public void DefineStyles(Document document)
        {
            Style style = document.Styles["Normal"];
            style.Font.Name = "Times New Roman";
            style.ParagraphFormat.SpaceBefore = 3;
            style.ParagraphFormat.SpaceAfter = 3;

            style = document.Styles.AddStyle("Normal-Bold", "Normal");
            style.Font.Bold = true;

            style = document.Styles["Heading2"];        
            style.Font.Size = 12;
            style.Font.Bold = true;
            style.ParagraphFormat.PageBreakBefore = false;
            style.ParagraphFormat.SpaceAfter = 18;
            style.ParagraphFormat.Alignment = ParagraphAlignment.Center;

            style = document.Styles["Heading3"];
            style.Font.Size = 10;
            style.Font.Bold = true;
            style.Font.Italic = true;
            style.ParagraphFormat.SpaceBefore = 6;
            style.ParagraphFormat.SpaceAfter = 3;

            //style = document.Styles[StyleNames.Header];
            //style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right);

            style = document.Styles[StyleNames.Footer];
            style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center);

            // Create a new style called TextBox based on style Normal
            //style = document.Styles.AddStyle("TextBox", "Normal");
            //style.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
            //style.ParagraphFormat.Borders.Width = 2.5;
            //style.ParagraphFormat.Borders.Distance = "3pt";
            //style.ParagraphFormat.Shading.Color = Colors.SkyBlue;

            // Create a new style called TOC based on style Normal
            //style = document.Styles.AddStyle("TOC", "Normal");
            //style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right, TabLeader.Dots);
            //style.ParagraphFormat.Font.Color = Colors.Blue;
        }
        public byte[] RenderXuatBangLuong(int m, int y,PhanCongViewModel source)
        {
            // Create a new MigraDoc document
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();
            //paragraph.Format.Font.Color = MigraDoc.DocumentObjectModel.Color.FromCmyk(100, 30, 20, 50);
            //paragraph.Format.Font.Color = MigraDoc.DocumentObjectModel.Color.FromCmyk(100, 30, 20, 50);           
            PhanCongXuatBangLuongGiaoVien(m, y, source,document);
            byte[] renderedBuffer;
            document.UseCmykColor = true;
            const bool unicode = true;
            const PdfFontEmbedding embedding = PdfFontEmbedding.Always;
            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);
            pdfRenderer.Document = document;
            pdfRenderer.RenderDocument();
            // To byte array
            MemoryStream stream = new MemoryStream();
            pdfRenderer.Save(stream, false);
            renderedBuffer = stream.ToArray();
            return renderedBuffer;
        }
        public void PhanCongXuatBangLuongGiaoVien(int m, int y,PhanCongViewModel bcvm, Document dc)
        {
            DefineStyles(dc);
            dc.LastSection.AddParagraph("CÔNG TY TNHH GIÁO DỤC VIỆT THƯƠNG MUSIC", "Heading2");
            Table info = new Table();
            info.Borders.Width = 0;
            Column infoColumn = info.AddColumn(Unit.FromCentimeter(4));
            info.Format.Alignment = ParagraphAlignment.Left;
            info.AddColumn(Unit.FromCentimeter(6));
            Row infoRow = info.AddRow();
            Cell infoCell = infoRow.Cells[0];
            infoCell.Format.Font.Bold = true;
            infoCell.AddParagraph("Bảng lương:");
            infoCell = infoRow.Cells[1];
            infoCell.AddParagraph("Tháng " + m.ToString() + "/" + y.ToString());

            infoRow = info.AddRow();
            infoCell = infoRow.Cells[0];
            infoCell.AddParagraph("Họ và tên giáo viên:");
            infoCell.Format.Font.Bold = true;
            infoCell = infoRow.Cells[1];
            infoCell.AddParagraph(bcvm.ListMoRong[0].TenGiaoVien);

            infoRow = info.AddRow();
            infoCell = infoRow.Cells[0];
            infoCell.AddParagraph("Mã giáo viên:");
            infoCell.Format.Font.Bold = true;
            infoCell = infoRow.Cells[1];
            infoCell.AddParagraph(bcvm.ListMoRong[0].Pc.MaGiaoVien.ToString());
            dc.LastSection.Add(info);

            Table table = new Table();
            table.Borders.Width = 0.75;

            Column column = table.AddColumn(Unit.FromCentimeter(2));
            column.Format.Alignment = ParagraphAlignment.Center;
            table.AddColumn(Unit.FromCentimeter(5));
            table.AddColumn(Unit.FromCentimeter(3));
            table.AddColumn(Unit.FromCentimeter(3));
            table.AddColumn(Unit.FromCentimeter(3));
            Row row = table.AddRow();
            row.Shading.Color = Colors.PaleGoldenrod;
            Cell cell = row.Cells[0];
            cell.AddParagraph("STT");
            cell = row.Cells[1];
            cell.AddParagraph("Ngày dạy");
            cell = row.Cells[2];
            cell.AddParagraph("ca học");
            cell = row.Cells[3];
            cell.AddParagraph("Lớp");
            cell = row.Cells[4];
            cell.AddParagraph("Lương/buổi");
            Converter cter = new Converter();
            for (int i = 0; i < bcvm.ListMoRong.Count; i++)
            {
                row = table.AddRow();
                cell = row.Cells[0];
                cell.AddParagraph((i+1).ToString());
                cell = row.Cells[1];
                cell.AddParagraph(bcvm.ListMoRong[i].Pc.NgayDay.Value.Date.ToString("dd/MM/yyyy"));
                cell = row.Cells[2];
                cell.AddParagraph(bcvm.ListMoRong[i].TenCaHoc);
                cell = row.Cells[3];
                cell.AddParagraph(bcvm.ListMoRong[i].TenLopHoc);
                cell = row.Cells[4];
                cell.AddParagraph(cter.GetStringMonney(bcvm.ListMoRong[i].TienCong.ToString()));
            }
            //table.SetEdge(0, 0, 2, 3, Edge.Box, BorderStyle.Single, 1.5, Colors.Black);
            dc.LastSection.Add(table);
            Paragraph pr = new Paragraph();
            pr.Format.Alignment = ParagraphAlignment.Right;
            pr.AddLineBreak();
            pr.AddFormattedText("Tổng số buổi dạy: ", "Normal");
            pr.AddFormattedText(bcvm.TotalMoRong.ToString(), "Normal-Bold");
            pr.AddLineBreak();
            pr.AddFormattedText("Tổng tiền lương: ", "Normal");
            pr.AddFormattedText(cter.GetStringMonney(bcvm.TongTienCong), "Normal-Bold");
            pr.AddLineBreak(); pr.AddLineBreak();
            pr.Format.Font.Size = 8;
            pr.AddText("Xuất ngày: " + DateTime.Now.ToString("dd/MM/yyyy"));
            dc.LastSection.Add(pr);
        }

        public byte[] Render()
        {
            byte[] renderedBuffer;

            //Document document = CreateDocument();
            Document document = new Document();
            
            document.UseCmykColor = true;
            const bool unicode = true;
            const PdfFontEmbedding embedding = PdfFontEmbedding.Always;

            // ========================================================================================

            // Create a renderer for the MigraDoc document.
            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);

            // Associate the MigraDoc document with a renderer
            pdfRenderer.Document = document;

            // Layout and render document to PDF
            pdfRenderer.RenderDocument();
            // To byte array
            MemoryStream stream = new MemoryStream();
            pdfRenderer.Save(stream, false);
            renderedBuffer = stream.ToArray();
            // Save the document...
            //const string filename = "HelloWorld.pdf";
            //pdfRenderer.PdfDocument.Save(filename);
            // ...and start a viewer.
            //Process.Start(filename);
            return renderedBuffer;
        }
    }
}